package com.fyzk.activity.trend;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanxs11 on 2017/7/20.
 * 排列3走势图
 */

public class PL5TrendView extends TrendBaseView {

    private int prizeCount = 5;
    private int numCount = 10;

    public PL5TrendView(Context context) {
        super(context);
    }

    public PL5TrendView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PL5TrendView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initData() {
        rowVisibleCount = 13;
        topPaddingCount = 1;
        leftPpaddingCount = 3;
        scale = 1.0f;
        rowCount = 10;
        super.initData();
    }

    @Override
    boolean isShowBottom(List<Drawinfor> infors) {
        return false;
    }

    @Override
    protected void onDrawTop(Canvas canvas, Rect rect) {
        paintBg.setColor(topBg);
        super.onDrawTop(canvas, rect);
        Rect topRect = new Rect(rect.left,rect.top, rect.left, rect.top);

        topRect.set(topRect.right, topRect.top, topRect.right+unitLength*3,topRect.top+unitLength);
        drawText(canvas, paintTop, "开奖号码", topRect);
        paintLine.setColor(lineColor);
        canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);

        for (int i = 3; i < rowCount; i++) {
            topRect.set(topRect.right, topRect.top, topRect.right+unitLength,topRect.top+unitLength);
            String num = ""+(i-3);
            drawText(canvas, paintTop, ""+num, topRect);
            if (i < rowCount-1) {
                canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
            }
        }
        paintLine.setColor(textDefault);
        canvas.drawLine(rect.left, rect.bottom-1, rect.right, rect.bottom-1, paintLine);
    }

    @Override
    protected void onDrawCenter(Canvas canvas, int left, int top) {
        Rect topRect = new Rect(left,top,left,top);

        int startId;
        int endId;
        int wei = 0;
        int shapColor = color_shap1;
        switch (chooseId) {
            case R.id.trend_choose_rb1:
                wei = 0;
                shapColor = color_shap1;
                break;
            case R.id.trend_choose_rb2:
                wei = 1;
                shapColor = color_shap2;
                break;
            case R.id.trend_choose_rb3:
                wei = 2;
                shapColor = color_shap3;
                break;
            case R.id.trend_choose_rb4:
                wei = 3;
                shapColor = color_shap1;
                break;
            case R.id.trend_choose_rb5:
                wei = 4;
                shapColor = color_shap2;
                break;
            default:
                break;
        }
        startId = prizeCount+numCount*wei;
        endId = prizeCount+numCount*(wei+1);

        List<Drawinfor> drawinfors = new ArrayList<>();

        for (int i = 0; i < columnCount; i++) {
            topRect.set(left,topRect.bottom,left,topRect.bottom+unitLength);
            drawCenterBg(canvas, left, top, i);

            topRect.set(topRect.right, topRect.top, topRect.right+unitLength*3,topRect.top+unitLength);
            String prizeNum = " - - -";
            if (infors.size() > 0) {
                prizeNum = "";
                for (int k = 0; k < prizeCount; k++) {
                    prizeNum += infors.get(i).get(k)+" ";
                }
                if (prizeNum.length() > 0) {
                    prizeNum = prizeNum.substring(0, prizeNum.length()-1);
                }
            }
            if (isTJ && i >= columnCount-4) {
                prizeNum = "";
                if (i == columnCount-4) {
                    paintLine.setColor(textDefault);
                    canvas.drawLine(topRect.left, topRect.top, topRect.right, topRect.top, paintLine);
                }
            }
            paintText.setColor(redColor);
            drawText(canvas, paintText, prizeNum, topRect);
            paintLine.setColor(lineColor);
            canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);

            for (int j = startId; j < endId; j++) {
                topRect.set(topRect.right, topRect.top, topRect.right+unitLength,topRect.top+unitLength);
                String text = ""+(j-prizeCount);
                if (infors.size() > 0) {
                    int missValue = infors.get(i).get(j);
                    if (missValue == 0) {

                        Drawinfor drawinfor = new Drawinfor();
                        drawinfor.text = ""+(j-prizeCount)%numCount;
                        drawinfor.rect = new Rect(topRect.left,topRect.top,topRect.right,topRect.bottom);
                        drawinfor.shapType = 0;
                        drawinfor.textColor = Color.WHITE;
                        drawinfor.shapColor = shapColor;
                        if (isTJ && i >= columnCount-4) {
                        } else {
                            drawinfors.add(drawinfor);
                        }

                    } else {
                        if (isYL) {
                            text = ""+missValue;
                        } else {
                            if (isTJ && i >= columnCount-4) {
                                text = ""+infors.get(i).get(j);
                            } else {
                                text = "";
                            }
                        }
                    }
                }
                paintText.setColor(textColorMiss);
                // 绘制统计数据
                if (isTJ && i >= columnCount-4) {
                    if (i == columnCount-4) {
                        paintLine.setColor(textDefault);
                        canvas.drawLine(topRect.left, topRect.top, topRect.right, topRect.top, paintLine);
                        paintText.setColor(color_showCount);
                    } else if (i == columnCount - 3) {

                        paintText.setColor(color_average);
                    } else if (i == columnCount - 2) {

                        paintText.setColor(color_maxMiss);
                    } else if (i == columnCount - 1) {

                        paintText.setColor(color_maxLian);
                    }
                }
                drawText(canvas, paintText, text, topRect);
                paintLine.setColor(lineColor_L);
                canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
            }
        }
        if (drawinfors.size() > 0) {
            drawShap(canvas, drawinfors, isZX);
        }
    }

    private void drawCenterBg(Canvas canvas, int left, int top, int i) {
        Rect bgRect = new Rect(left, top+i*unitLength, drawWidth, top+(i+1)*unitLength);
        if (i % 2 == 0) {
            paintBg.setColor(bgBai);
        } else {
            paintBg.setColor(bgHui);
        }

        canvas.drawRect(bgRect, paintBg);
        paintLine.setColor(textDefault);
        canvas.drawLine(bgRect.left, bgRect.bottom, bgRect.right, bgRect.bottom, paintLine);
    }


    @Override
    public void onChooseClick(int id) {
        chooseId = id;
        setRowCount(13);
        setScrollY(drawHeight - getHeight());
        recording();
        invalidate();
    }

    @Override
    public void onChangePayType(int id) {
    }

    @Override
    public void setInfors(List<List<Integer>> infors, List<String> phaseInfos, int chooseId) {
        super.setInfors(infors, phaseInfos, chooseId);
        onChooseClick(chooseId);
    }
}
